package com.hgw.ossdemo.controller;

import com.hgw.ossdemo.common.FileResult;
import com.hgw.ossdemo.service.OssUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Description: OSS 文件上传
 *
 * @author itLaboratory-yanAn
 * @date 2022/10/7 21:45
 */
@RestController
@RequestMapping("oss")
public class OssController {

    @Autowired
    OssUploadService ossUploadService;

    /**
     * 文件上传至OSS
     *
     * @param multipartFile MultipartFile
     * @param dir       请求域,获取 dir
     * @return 文件上传与下载返回体
     */
    @PostMapping("/upload")
    public FileResult Upload(@RequestParam("dir") String dir, @RequestParam("file") MultipartFile multipartFile) {
        FileResult fileResult = new FileResult();
        if (multipartFile.isEmpty()) {
            fileResult.setSuccess(false);
            return fileResult;
        }
        return ossUploadService.upload(multipartFile, dir);
    }

}
